package cn.iocoder.yudao.framework.common.pojo;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.apache.commons.lang.StringUtils.split;

@ApiModel("分页参数")
public class PageParam implements Serializable {

    private static final Integer PAGE_NO = 1;
    private static final Integer PAGE_SIZE = 10;
    public static final SortingField DEFAULT_SORT_FIELD = new SortingField();

    public static final ErrorCode PARAM_ERROR= new ErrorCode(1004003456, "参数错误");
//    public static final String injStr = "'|and|exec|insert|select|delete|update|count|*|%" +
//            "|chr|mid|master|truncate|char|declare|;|or|-|+|,";
    private static final Pattern pattern = Pattern.compile("\\b(and|exec|insert|select|drop|grant|alter|delete|update|count|chr|mid|master|truncate|char|declare|or)\\b|(\\*|;|\\+|'|%)");

    public static final List<SortingField> DEFAULT_SORTING_FIELDS = new ArrayList<SortingField>(1) {{
        add(new SortingField());
    }};

    @ApiModelProperty(value = "页码，从 1 开始", required = true, example = "1")
    @NotNull(message = "页码不能为空")
    @Min(value = 1, message = "页码最小值为 1")
    private Integer pageNo = PAGE_NO;

    @ApiModelProperty(value = "每页条数，最大值为 100", required = true, example = "10")
    @NotNull(message = "每页条数不能为空")
    @Min(value = 1, message = "页码最小值为 1")
    @Max(value = 100, message = "页码最大值为 100")
    private Integer pageSize = PAGE_SIZE;

    @ApiModelProperty(value = "排序规则(多字段)", example = "[{\"field\":\"updateTime\",\"order\":\"desc\"},{\"field\":\"createTime\",\"order\":\"desc\"}]")
    private List<SortingField> sortingFields = DEFAULT_SORTING_FIELDS;

    @ApiModelProperty(value = "排序规则(单字段)", example = "{\"field\":\"updateTime\",\"order\":\"desc\"}")
    private SortingField sortField;

    public int getOffset() {
        return this.pageNo > 0 ? (this.pageNo - 1) * this.pageSize : 0;
    }

    public int getSize() {
        return this.pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<SortingField> getSortingFields() {
        return sortingFields;
    }

    public void setSortingFields(List<SortingField> sortingFields) {
        if(!CollectionUtils.isEmpty(sortingFields)){
            for (SortingField sortingField : sortingFields) {
                if(null!=sortingField){
                    sqlInj(sortingField.getField());
                    sqlInj(sortingField.getOrder());
                }
            }
        }
        this.sortingFields = sortingFields;
    }

    public SortingField getSortField() {
        return sortField;
    }

    public void setSortField(SortingField sortField) {
        if(null!=sortField){
            sqlInj(sortField.getField());
            sqlInj(sortField.getOrder());
        }
        this.sortField = sortField;
    }

    public static void sqlInj(String str){
//        if(StringUtils.isNotBlank(str)){
//            String injStra[] = split(injStr,"|");
//            for (int i=0 ; i < injStra.length ; i++ ){
//                if (str.indexOf(injStra[i])>=0){
//                    throw exception(PARAM_ERROR);
//                }
//            }
//        }
        if(StringUtils.isNotBlank(str)){
            Matcher isMatch = pattern.matcher(str.toLowerCase());
            if(isMatch.find()){
                throw exception(PARAM_ERROR);
            }
        }
    }
}
